package com.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;

import com.dao.BaseDao;

public class BaseDaoImpl<T> implements BaseDao<T> {
	protected Class<T> entityClass;
	// protected SqlMapClient sqlMapClient;
	
	private static final String path = "com.po.";
	@Resource(name="sqlSession")
	protected SqlSession session;

	public BaseDaoImpl() {
		this.entityClass = getDAOClass();
	}

	@Override
	public int Save(T t) {
		// TODO Auto-generated method stub
		return session.insert(getMethodPath("save"),t);
	}

	@Override
	public int Del(int id) {
		// TODO Auto-generated method stub
		return session.delete(getMethodPath("delete"), id);
	}

	@Override
	public int Update(T t) {
		// TODO Auto-generated method stub
		System.out.println("update");
		return session.update(getMethodPath("update"), t);
	}

	@Override
	public T getById(Integer id) {
		// TODO Auto-generated method stub
		return session.selectOne(getMethodPath("selectByID"),id);
	}

	@Override
	public List<T> findAll() {
		// TODO Auto-generated method stub
		return session.selectList(getMethodPath("list"));
	}

	private Class<T> getDAOClass() {
		ParameterizedType type = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		Type[] types = type.getActualTypeArguments();
		Class<T> clazz = (Class<T>) types[0];
		return clazz;
	}
	
	
	private String getMethodPath(String methodType){
        return path + entityClass.getSimpleName() + "Mapper." + methodType;
    }
	

}
